package com.pandora.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pandora.dao.LocationDao;
import com.pandora.db.SqlConnection;
import com.pandora.entity.LocationEntity;

public class LocationDaoImpl implements LocationDao {
	
	protected Connection conn = null;

	public List<LocationEntity> getLocationList(int userid) {
		conn = SqlConnection.getConnection();
		PreparedStatement pstat = null;
		ResultSet resultset = null;
		String sql = "select * from location where userid=?";
		List<LocationEntity> list = new ArrayList<LocationEntity>();
		LocationEntity location = null;
		try {
			pstat = conn.prepareStatement(sql);
			resultset = pstat.executeQuery();
			while (resultset.next()) {
				location = new LocationEntity();
				location.setId(resultset.getInt("l_id"));
				location.setLongitude(resultset.getDouble("l_longitude"));
				location.setLatitude(resultset.getDouble("l_latitude"));
				location.setAddress(resultset.getString("l_address"));
				location.setUserid(resultset.getInt("userid"));
				list.add(location);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SqlConnection.getRelease(conn, pstat, resultset);
		}
		return null;
	}
	
	public List<Map<String, Object>> getAllLocation(int userid) {
		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		conn = SqlConnection.getConnection();
		String sql = "select * from location where userid=?";
		PreparedStatement pstat = null;
		ResultSet resultset = null;
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, 1);
			resultset = pstat.executeQuery();
			while (resultset.next()) {
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("id", resultset.getInt("l_id"));
				item.put("longitude", resultset.getDouble("l_longitude"));
				item.put("latitude", resultset.getDouble("l_latitude"));
				item.put("address", resultset.getString("l_address"));
				item.put("datetime", resultset.getString("l_time"));
				item.put("userid", resultset.getInt("userid"));
				itemsList.add(item);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			SqlConnection.getRelease(conn, pstat, resultset);
		}
		return itemsList;
	}
}
